from model.sql import Table, Field
from stock_service import prepare


class gene_stocks(Table):
    id = Field('id', 'integer', primary_key=True)
    stock_code = Field('stock_code', 'varchar(10)', primary_key=False)
    gene_code = Field('gene_code', 'varchar(10)', primary_key=False)


class genes(Table):
    code = Field('code', 'varchar(10)', primary_key=True)
    market = Field('market', 'int', primary_key=False)
    name = Field('name', 'varchar(30)', primary_key=False)


class genes_kl(Table):
    id = Field('id', 'integer', primary_key=True, auto_inc=True)
    code = Field('code', 'varchar(10)', primary_key=False)
    date = Field('date', 'varchar(12)', primary_key=False)
    open = Field('open', 'varchar(15)', primary_key=False)
    close = Field('close', 'varchar(15)', primary_key=False)
    low = Field('low', 'varchar(15)', primary_key=False)
    high = Field('high', 'varchar(15)', primary_key=False)
    total_volume = Field('total_volume', 'int', primary_key=False)
    total_amount = Field('total_amount', 'varchar(40)', primary_key=False)


class stock(Table):
    code = Field('code', 'varchar(10)', primary_key=True, auto_inc=True)
    market = Field('market', 'int', primary_key=False)
    name = Field('name', 'varchar(30)', primary_key=False)


class stock_kl(Table):
    id = Field('id', 'integer', primary_key=True, auto_inc=True)
    code = Field('code', 'varchar(10)', primary_key=False)
    date = Field('date', 'varchar(12)', primary_key=False)
    open = Field('open', 'varchar(15)', primary_key=False)
    close = Field('close', 'varchar(15)', primary_key=False)
    low = Field('low', 'varchar(15)', primary_key=False)
    high = Field('high', 'varchar(15)', primary_key=False)
    total_volume = Field('total_volume', 'int', primary_key=False)
    total_amount = Field('total_amount', 'varchar(40)', primary_key=False)



if __name__ == '__main__':

    def get_fields(t_name, cur):
        sql = 'pragma table_info(%s);' % t_name
        fields = cur.execute(sql)
        class_temp = ["class %s(Table):" % t_name]
        for field in fields:
            class_temp.append("    %s = Field('%s', '%s', primary_key=%s)" % (field[1], field[1], field[2],
                                                                               'True' if field[5] == 1 else 'False'))
        print('\n'.join(class_temp))

    with prepare.get_conn() as conn:
        c = conn.cursor()
        records = c.execute('SELECT name FROM sqlite_master WHERE type=\'table\' ORDER BY name;')
        records = [record for record in records]
        try:
            for record in records:
                table_name, = record
                if table_name != 'sqlite_sequence':
                    get_fields(table_name, c)
        except Exception as e:
            print(e)

